#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    ui->progressBar->setVisible(false);
    ui->action_transform->setEnabled(false);
    ui->tableWidget->installEventFilter(this);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_action_open_triggered()
{
    getSourceFileNames();
    uniqSourceFileNames();
    showSourceFileNames();
    return;
}

void MainWindow::getSourceFileNames()
{
    sourceFileNames << QFileDialog::getOpenFileNames(
                this,trUtf8("选择一个或多个将要转换的文件"),
                QDir::currentPath(),
                trUtf8("脚本文件 (*.py *.sh *.js);;全部文件 (*)"),
                0,QFileDialog::DontUseNativeDialog);
}

bool MainWindow::eventFilter(QObject *obj, QEvent *event) {
    if (obj == ui->tableWidget)
    {
        if (event->type() == QEvent::DragEnter)
        {
            QDragEnterEvent *dee = dynamic_cast<QDragEnterEvent *>(event);
            dee->acceptProposedAction();
            return true;
        } else if (event->type() == QEvent::Drop) {
            QDropEvent *de = dynamic_cast<QDropEvent *>(event);
            QList<QUrl> urls = de->mimeData()->urls();

            if (urls.isEmpty())
            {
                return true;
            }
            for(int i=0;i<urls.count();++i)
            {
                sourceFileNames<<urls.at(i).toLocalFile();
            }
            uniqSourceFileNames();
            showSourceFileNames();
            return true;
        }
        return false;
    }
    return QMainWindow::eventFilter(obj, event);
}
void MainWindow::uniqSourceFileNames()
{
    sourceFileNames = sourceFileNames.toSet().toList();
    return;
}

void MainWindow::showSourceFileNames()
{
    if(!sourceFileNames.isEmpty())
    {
        ui->action_transform->setEnabled(true);
    }else{
        ui->action_transform->setEnabled(false);
    }
    for(int i=0;i<sourceFileNames.count();++i)
    {
        ui->tableWidget->setRowCount(sourceFileNames.count());
        QFileInfo fileInfo(sourceFileNames.at(i));

        QTableWidgetItem *fileItem = new QTableWidgetItem(fileInfo.fileName());
        if(sourceFileNames.at(i).toLower().endsWith(".py"))
        {
            fileItem->setIcon(QIcon::fromTheme("text-x-python"));
        }else if(sourceFileNames.at(i).toLower().endsWith(".js")){
            fileItem->setIcon(QIcon::fromTheme("application-javascript"));
        }else if(sourceFileNames.at(i).toLower().endsWith(".sh")){
            fileItem->setIcon(QIcon::fromTheme("application-x-shellscript"));
        }else{
            fileItem->setIcon(QIcon::fromTheme("text-x-plain"));
        }
        ui->tableWidget->setItem(i,0,fileItem);

        QTableWidgetItem *absolutePathItem = new QTableWidgetItem(fileInfo.absolutePath());
        absolutePathItem->setIcon(QIcon::fromTheme("folder"));
        ui->tableWidget->setItem(i,1,absolutePathItem);

        QTableWidgetItem *sizeItem = new QTableWidgetItem(QString::number(fileInfo.size())+QString(" B"));
        ui->tableWidget->setItem(i,2,sizeItem);

        QTableWidgetItem *statusItem = new QTableWidgetItem(trUtf8("未转换"));
        statusItem->setIcon(QIcon::fromTheme("task-accepted"));
        ui->tableWidget->setItem(i,3,statusItem);
    }
    return;
}

void MainWindow::on_action_help_triggered()
{
    QMessageBox::information(this,trUtf8("帮助"),
                             trUtf8("“张龙赵虎王朝马汉！” “在！”“王朝马汉张龙赵虎！” “在！“ “张龙马汉王朝赵虎！” "
                                    "“在！” “赵虎……” “包大人你到底有什么事？？！！” “没事，就是看看哪个顺口一些~”\n"
                                    "Email: jhello@mail.dlut.edu.cn"));
    return;
}

void MainWindow::on_action_clear_triggered()
{
    sourceFileNames.clear();
    ui->tableWidget->setRowCount(sourceFileNames.count());
    ui->progressBar->setVisible(false);
    showSourceFileNames();
    return;
}

void MainWindow::on_action_transform_triggered()
{
    ui->progressBar->reset();
    ui->progressBar->setMinimum(0);
    ui->progressBar->setMaximum(sourceFileNames.count());
    ui->progressBar->setVisible(true);
    for(int i=0;i<sourceFileNames.count();++i)
    {
        int argc = 2,result;
        char a[] = "embed", *b;
        b = sourceFileNames.at(i).toUtf8().data();
        char *argv[] = {a,b};
        result = decorate(argc,argv);
        if(result == EXIT_SUCCESS)
        {
            ui->tableWidget->item(i,3)->setIcon(QIcon::fromTheme("task-complete"));
            ui->tableWidget->item(i,3)->setText(trUtf8("完成"));
            if(ui->hideTransformedCheckBox->checkState() == Qt::Checked)
            {
                ui->tableWidget->hideRow(i);
            }
        }else if(result == EXIT_FAILURE){
            ui->tableWidget->item(i,3)->setIcon(QIcon::fromTheme("task-reject"));
            ui->tableWidget->item(i,3)->setText(trUtf8("失败"));
        }
        ui->progressBar->setValue(i+1);
    }
    if(ui->completeAndExitCheckBox->checkState() == Qt::Checked)
    {
        bool flag = false;
        for(int i=0;i<sourceFileNames.count();++i)
        {
            if(ui->tableWidget->item(i,3)->text() != trUtf8("完成"))
            {
                flag = true;
            }
        }
        if(flag == false)
        {
            qApp->exit(EXIT_SUCCESS);
        }
    }
    return;
}

void MainWindow::on_hideTransformedCheckBox_stateChanged(int arg1)
{
    if(arg1 == Qt::Unchecked)
    {
        for(int i=0;i<sourceFileNames.count();++i)
        {
            if(ui->tableWidget->item(i,3)->text() == trUtf8("完成"))
            {
                ui->tableWidget->setRowHidden(i,false);
            }
        }
    }else if(arg1 ==Qt::Checked){
        for(int i=0;i<sourceFileNames.count();++i)
        {
            if(ui->tableWidget->item(i,3)->text() == trUtf8("完成"))
            {
                ui->tableWidget->setRowHidden(i,true);
            }
        }
    }
    return;
}

void MainWindow::on_completeAndExitCheckBox_stateChanged(int arg1)
{
    if(arg1 == Qt::Checked)
    {
        bool flag = false;
        for(int i=0;i<sourceFileNames.count();++i)
        {
            if(ui->tableWidget->item(i,3)->text() != trUtf8("完成"))
            {
                flag = true;
            }
        }
        if(flag == false)
        {
            qApp->exit(EXIT_SUCCESS);
        }
    }
    return;
}
